package framework.util.codeset.controller;

import framework.util._BeanUtil;
import framework.util.codeset.bo.SysCodeset;
import framework.util.codeset.bo.SysCodesetDetail;
import framework.common.ResponseVo;
import framework.util.codeset.service.SysCodesetDetailService;
import framework.util.controller.BaseController;
import framework.util.layui._LayuiUtil;
import framework.util.queryFilter.service.impl.QueryFilter;
import framework.util.queryFilter.vo.Where;
import frameworkExt.util.controller.BaseControllerExt;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 吴宇亮 on 2019/5/26 13:24
 */
@Controller
@RequestMapping("/framework/sysCodesetDetail.do")
public class SysCodesetDetailController extends BaseControllerExt {

    @Resource
    private SysCodesetDetailService service;

    @ResponseBody
    @RequestMapping(params = "method=listByCodesetLid")
    public ResponseVo listByCodesetLid(HttpServletRequest request, HttpServletResponse response){
        String clid = request.getParameter(SysCodesetDetail.CLID);
        if(StringUtils.isBlank(clid)){
            return sendSuccess();
        }

        QueryFilter filter = new QueryFilter(_LayuiUtil.FIRST_PAGE, Integer.MAX_VALUE);
        filter.where(SysCodesetDetail.CLID, Where.eq, clid);
        List<SysCodesetDetail> list = service.getAll(filter);
        return sendSuccess(list);
    }

    @ResponseBody
    @RequestMapping(params = "method=listByCodesetEnname")
    public ResponseVo listByCodesetEnname(HttpServletRequest request, HttpServletResponse response){
        String enname = request.getParameter(SysCodeset.EN_NAME);
        if(StringUtils.isBlank(enname)){
            return sendFail("参数错误");
        }

        List<SysCodesetDetail> detailList = BaseController.CODE.get(enname).getDetailList();
        return sendSuccess(detailList);
    }

    @ResponseBody
    @RequestMapping(params = "method=add")
    public ResponseVo add(SysCodesetDetail bo, HttpServletRequest request, HttpServletResponse response){
        service.add(bo);
        return sendSuccess();
    }

    @RequestMapping(params = "method=toEditOrDetail")
    public ModelAndView toEditOrDetail(ModelAndView model, HttpServletRequest request, HttpServletResponse response) {
        String lid = request.getParameter("lid");
        String pageUrl = request.getParameter("pageUrl");
        if(StringUtils.isAnyBlank(lid, pageUrl)){
            return null;
        }

        SysCodesetDetail bo = service.get(Integer.valueOf(lid));
        model.addObject("bo", bo);
        model.setViewName(pageUrl);
        return model;
    }

    @ResponseBody
    @RequestMapping(params = "method=update")
    public ResponseVo update(ModelAndView model, HttpServletRequest request, HttpServletResponse response){
        String lid = request.getParameter("lid");
        if(StringUtils.isBlank(lid)){
            return sendFail("参数错误");
        }

        try {
            SysCodesetDetail oldBo = service.get(Integer.valueOf(lid));
            _BeanUtil.copyPropertiesFromRequest(oldBo, request);
            service.update(oldBo);
            return sendSuccess();
        } catch (IllegalAccessException | InvocationTargetException e) {
            e.printStackTrace();
            return sendError(e.getMessage());
        }
    }

    @ResponseBody
    @RequestMapping(params = "method=delete")
    public ResponseVo delete(ModelAndView model, HttpServletRequest request, HttpServletResponse response) {
        String lids = request.getParameter("lids");
        if (StringUtils.isBlank(lids)) {
            return sendFail("参数错误");
        }

        List<SysCodesetDetail> list = new ArrayList<>();
        for(String lid : lids.split(",")){
            SysCodesetDetail bo = service.get(Integer.valueOf(lid));
            list.add(bo);
        }
        service.removeBatch(list);
        return sendSuccess();
    }

}
